package gui;

import gui.GUIConstants.SelectionStatus;
import gui.GUIConstants.ViewState;

import java.awt.event.MouseEvent;

import javax.swing.event.MouseInputAdapter;

import command.Command;
import command.CommandBroadcaster;
import command.CommandGenerator;


import core.Battle;
import core.Constants;
import core.Core;
import core.Battle.BattleState;
import core.Constants.ConnectionType;

public class CenterPanelBattleListener extends MouseInputAdapter{
	private BattleView battleView = BattleView.getInstance();
	private Battle battle = Battle.getInstance();
	
	public void mouseClicked(MouseEvent e) {
		switch(battleView.selectionStatus){
		case NO_SELECTION:
			//Select a Card from a SelectionSphere
			for(SelectionSphere s : battleView.selectionSpheres){
				if(s.contains(e.getX(), e.getY())){
					CardViewPanel.getInstance().setShownCard(s.getCard());
					// if card is yours
					if(s.getCard().getOwner() == Core.getInstance().getThisPlayer()){
						battleView.setSelectedCard(s.getCard());
						battleView.selectionStatus=SelectionStatus.CARD_SELECTED;
						s.setHighlighted(true);
					}
				}
			}
			break;	
		case CARD_SELECTED:
			if(e.getButton()==MouseEvent.BUTTON3){
				battleView.selectionStatus=SelectionStatus.NO_SELECTION;
				SelectionSphereUtil.removeHighlight(battleView.selectionSpheres);
			}
			else{			
				//Tell selected card to target clicked card
				for(SelectionSphere s : battleView.selectionSpheres){
					if(s.contains(e.getX(), e.getY())){
						// if card is enemy
						if(s.getCard().getOwner() != Core.getInstance().getThisPlayer()){
							Command com = CommandGenerator.getInstance().generateBattleTargetCommand(
									battleView.getSelectedCard(), s.getCard());
							if(com != null)
								CommandBroadcaster.sendCommand(com);
							battleView.selectionStatus=SelectionStatus.NO_SELECTION;
							SelectionSphereUtil.removeHighlight(battleView.selectionSpheres);
						}
					}
				}
			}
			break;
			
		}

		if(battleView.returnToMapButton.contains(e.getX(), e.getY())){
			if(battle.battleState == BattleState.HASBATTLED){
				GUICore.getInstance().currentView=ViewState.MAP;
			}		
		}
		if(battleView.leftReadyButton.contains(e.getX(), e.getY())){
			if(!battle.getIsReadyToFight(0) && battle.getPlayer(0) == Core.getInstance().getThisPlayer()){
				Command com = CommandGenerator.getInstance().generateBattleStartCommand();
				if(com != null)
					CommandBroadcaster.sendCommand(com);
			}
		}
		if(battleView.rightReadyButton.contains(e.getX(), e.getY())){
			if(!battle.getIsReadyToFight(1) && battle.getPlayer(1) == Core.getInstance().getThisPlayer()){
				Command com = CommandGenerator.getInstance().generateBattleStartCommand();
				if(com != null)
					CommandBroadcaster.sendCommand(com);
			}
		}
		if(Constants.connectionState == ConnectionType.DISCONNECTED){
			if(battleView.setPlayerOneButton.contains(e.getX(), e.getY())){
				Core core=Core.getInstance();
				core.setThisPlayer(core.getPlayers().get(0));
			}
			if(battleView.setPlayerTwoButton.contains(e.getX(), e.getY())){
				Core core=Core.getInstance();
				core.setThisPlayer(core.getPlayers().get(1));
			}
		}
		GUICore.getInstance().updateAll();
		
	}
}
